import url from "url";
import methods from "methods";
function Router() {
  this.stack = [];
}
methods.forEach((method) => {
  Router.prototype[method] = function (path, handler) {
    let layer = {
      path,
      method,
      handler,
    };
    this.stack.push(layer);
  };
});
Router.prototype.handle = function (req, res, done) {
  const { pathname, query } = url.parse(req.url, true);
  const method = req.method.toLowerCase();
  for (let i = 0; i < this.stack.length; i++) {
    let {
      path: layerPath,
      method: layerMethod,
      handler: layerHandler,
    } = this.stack[i];

    if (pathname === layerPath && method === layerMethod) {
      return layerHandler(req, res);
    }
  }
  done();
};

export default Router;
